package de.wellenblau.morphing
{

	import caurina.transitions.Tweener;
	
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Cube;


	public class MorphCube extends Cube
	{
		private var avgSize:Number;
		private var minDis:Number;
		private var maxDis:Number;
		private var completeCount:int = 0;
		
		public function MorphCube(materials:MaterialsList, width:Number=500, depth:Number=500, height:Number=500, segmentsS:int=1, segmentsT:int=1, segmentsH:int=1, insideFaces:int=0, excludeFaces:int=0)
		{
			super(materials, width, depth, height, segmentsS, segmentsT, segmentsH, insideFaces, excludeFaces);
			
			avgSize = (width + depth + height) / 6;
			minDis = Math.min(width,depth,height) / 2;
			maxDis = Math.sqrt(
				(width/2)*(width/2) + 
				(depth/2)*(depth/2)+ 
				(height/2)*(height/2)
			);
		}
		
		public function scaleTo(s:Number):void{
			
			if(completeCount == 0){
				completeCount = geometry.vertices.length;
				
				for each(var v:Vertex3D in geometry.vertices){
					var distToZero:Number = Math.sqrt(v.x*v.x + v.y*v.y + v.z*v.z) - minDis;
					
					var timeDis:Number = 0;
					if(s > 1){ 
						timeDis = ((1.8 - distToZero / (maxDis-minDis))*2.1);
					}
					else{
						timeDis = (distToZero / (maxDis-minDis) + 0.8 )*2.1;
					} 
					//completeCount++;
			
					Tweener.addTween(v,{
						x:v.x*s,
						y:v.y*s,
						z:v.z*s,
						time:timeDis,
						transition:"easeInOutBounce",
						onComplete:function():void{completeCount--}
					});
					
				}
				
				minDis *= s;
				maxDis *= s;
			}
		}
		
		public function  get isMorphing():Boolean{
			if(completeCount == 0) return false;
			else return true;		
		}
		
	}
}